<template>
  <div>
    <ToolTip :content="row.accountName" style="color:#406EFF;cursor:pointer" @click.native="toAccountDetail(row.accountId)"/>
    <div class="flex-center" style="color: #999; margin: 8px 0;">
      <platformIcon :data="row.platformName" />
      <ToolTip :content="row.platformName" style="color: #333" />
    </div>
    <div class="flex-center">
      <ToolTip v-if="row.link" :content="row.link" is-can-click style="color:#406eff; max-width: 90px;" @handelClick="openLink(row.link)" />
      <div v-else>--</div>
      <i
        class="el-icon-edit-outline"
        style="margin-left:5px;font-size:16px;color: #406eff;cursor:pointer;"
        @click="editLink(row)"/>
    </div>
    <editDialog ref="editLinkDialog" :allow-disable="false" :maxlength="256" title="报价平台主页链接" placeholder="请输入链接" @submit="handleEditLink"/>
  </div>
</template>
<script>
import platformIcon from '@/components/Common/platformIcon';
import editDialog from './editDialog';
import { hasRouteAuth } from '@/utils/permission';
import { editPriceItemLink } from '@/api/flyPrice';
export default {
  components: { platformIcon, editDialog },
  props: {
    row: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      editLinkId: ''
    };
  },
  methods: {
    // 跳转账号详情
    toAccountDetail(id) {
      if (!hasRouteAuth('medium:resource:admin')) {
        this.$message.warning('暂无资源可读权限，请联系管理员开通');
        return;
      }
      if (id) {
        const url = this.$router.resolve({
          name: 'detailResource',
          params: { resourceId: id }
        });
        window.open(url.href, '_blank');
      }
    },
    async handleEditLink(val) {
      const params = {
        link: val,
        id: this.editLinkId
      };
      const res = await editPriceItemLink(params);
      if (res.code === 200) {
        this.$message.success('修改成功');
        this.$emit('updateInfo');
      }
    },
    editLink(row) {
      this.editLinkId = row.id;
      this.$refs['editLinkDialog'].openDialog(row.link);
    },
    openLink(val) {
      if (/^(www)/i.test(val)) {
        val = `https://${val}`;
        window.open(val);
        return;
      } else if (val && this.testUrl(val)) {
        window.open(val);
        return;
      }
      this.$message.warning('无效的链接');
    },
    testUrl(url) {
      const reg = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
      return !!reg.test(url);
    }
  }
};
</script>
